package com.sjc.geekDesignPattern.observer;

import com.sjc.geekDesignPattern.observer.myEventBus.EventBus;
import com.sjc.geekDesignPattern.observer.myEventBus.Subscribe;

import java.util.ArrayList;
import java.util.List;

class XMsg {}
class YMsg {}
class ZMsg {}

class AObserver {
    @Subscribe
    public void f(XMsg event){
        System.out.println(this.getClass().getName() +" A-f "+ event.getClass().getName());
    }
}
class BObserver {
    @Subscribe
    public void f1(YMsg event) {
        System.out.println(this.getClass().getName()  +" B-f1 "+  event.getClass().getName());
    }
    @Subscribe
    public void f2(ZMsg event) {
        System.out.println(this.getClass().getName()  +" B-f2 "+  event.getClass().getName());
    }
}
class CObserver {
    @Subscribe
    public void f3(ZMsg event) {
        System.out.println(this.getClass().getName()  +" C-f3 "+  event.getClass().getName());
    }
}

public class DemoEventBus {
    private EventBus eventBus;

    public DemoEventBus() {
        this.eventBus = new EventBus();
    }

    public void post(Object event) {
        System.out.println("post: " + event.getClass().getName());
        eventBus.post(event);
    }

    public void setRegObservers(List<Object> observers) {
        for (Object observer : observers) {
            eventBus.register(observer);
        }
    }

    public static void main(String[] args) {
        DemoEventBus demo = new DemoEventBus();
        List<Object> observers = new ArrayList<>();
        observers.add(new AObserver());
        observers.add(new BObserver());
        observers.add(new CObserver());

        demo.setRegObservers(observers);

        demo.post(new XMsg());
        demo.post(new YMsg());
        demo.post(new ZMsg());
    }
}
